package com.aim.analysis.records;

import com.aim.analysis.records.bean.AlertData;
import com.aim.analysis.records.bean.PerformanceData;
import com.aim.azure.AzureGameRecordEntity;
import com.aim.game.MC;
import com.aim.util.filters.Filter;
import com.google.gson.Gson;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 6/4/14
 * Time: 9:29 PM
 * To change this template use File | Settings | File Templates.
 */
public class GameRecord {

    public GameKey gameKey;
    public Date date;
    public int time;
    public int sequenceNumber;
    public int score;
    public MC mechanicalCondition;
    public int fuelLevel;
    public RecordType recordType;

    // data per record type
    public PerformanceData performanceData;
    private AlertData alertData;



    public GameRecord(GameKey gameKey, Date date, int time, int sequenceNumber, int score, MC mechanicalCondition, int fuelLevel, RecordType recordType) {
        this.gameKey = gameKey;
        this.date = date;
        this.time = time;
        this.sequenceNumber = sequenceNumber;
        this.score = score;
        this.mechanicalCondition = mechanicalCondition;
        this.fuelLevel = fuelLevel;
        this.recordType = recordType;
    }

    public GameRecord(GameRecord record) {
        this.gameKey = record.gameKey;
        this.date = record.date;
        this.time = record.time;
        this.sequenceNumber = record.sequenceNumber;
        this.score = record.score;
        this.mechanicalCondition = record.mechanicalCondition;
        this.fuelLevel = record.fuelLevel;
        this.recordType = record.recordType;
    }





    public PerformanceData getPerformanceData() {
        return performanceData;
    }

    public void setPerformanceData(PerformanceData performanceData) {
        this.performanceData = performanceData;
    }

    public AlertData getAlertData() {
        return alertData;
    }

    public void setAlertData(AlertData alertData) {
        this.alertData = alertData;
    }

    private static Gson gson = new Gson();

    public static GameRecord fromAzureRecord(AzureGameRecordEntity ar) {
        int gameNumber = Integer.parseInt(ar.getGameNumber());
        GameKey key = new GameKey(ar.PartitionKey, ar.GameDescription, gameNumber);


        int sequenceNumber = Integer.parseInt(ar.getSequenceNumber());
        int score = Integer.parseInt(ar.getScore());
        MC mechanicalCondition = MC.values()[Integer.parseInt(ar.getMechanicalCondition())];
        int fuelLevel = ar.getFuelLevel() == null ? 0 : Integer.parseInt(ar.getFuelLevel());
        RecordType recordType = RecordType.byCode(Integer.parseInt(ar.getMsgType()));

        int time = recordType == RecordType.gameStarted ? 0 : Integer.parseInt(ar.getTime());

        GameRecord gameRecord = new GameRecord(key, ar.Timestamp, time, sequenceNumber, score, mechanicalCondition, fuelLevel, recordType);

        switch (recordType) {
            case meteorSummery:
                PerformanceData performanceData = gson.fromJson(ar.getContent(), PerformanceData.class);
                gameRecord.setPerformanceData(performanceData);
                break;
            case alertBeforeDisplayed:
                AlertData alertData = gson.fromJson(ar.getContent(), AlertData.class);
                gameRecord.setAlertData(alertData);
                break;
        }

        return gameRecord;
    }

    public static Filter<GameRecord> dummyFilter(){
        return new Filter<GameRecord>() {
            @Override
            public boolean accept(GameRecord record) {
                return true;
            }
        };
    }


}
